//
//  LJLMyCollectionVC.m
//  聆听FM
//
//  Created by dllo on 16/6/15.
//  Copyright © 2016年 Dark. All rights reserved.
//

#import "LJLMyCollectionVC.h"
#import "LTDataBase.h"
#import "LTAnchorCollectTableViewCell.h"
#import "SFDraggableDialogView.h"
#import "YWAnchorIntroduceViewController.h"

@interface LJLMyCollectionVC ()<UITableViewDelegate, UITableViewDataSource,SFDraggableDialogViewDelegate>
@property(nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIImageView *imageView;

@property (nonatomic, retain) LTAnchorModel *anchorModel;
@property (nonatomic, retain) UIImageView *backImageView;
@end

@implementation LJLMyCollectionVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我关注的主播";
    self.navigationController.navigationBar.translucent = YES;
    self.imageView = [[UIImageView alloc] init];
    [self creatView];
    [self getData];
}


- (void)creatView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.separatorStyle = 0;
    self.tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"LTAnchorCollectTableViewCell" bundle:nil] forCellReuseIdentifier:@"anchor"];
    
    UIView *foot = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 70 )];
    self.tableView.tableFooterView = foot;
    self.backImageView = [[UIImageView alloc] initWithFrame:self.tableView.bounds];
    self.backImageView.image = [UIImage imageNamed:@"主播"];
    self.backImageView.hidden = YES;
    [self.view addSubview:_backImageView];
    
    

}
- (void)showViewWith:(LTAnchorModel *)model
{
    self.anchorModel = model;
    SFDraggableDialogView *dialogView = [[[NSBundle mainBundle] loadNibNamed:@"SFDraggableDialogView" owner:self options:nil] firstObject];
    dialogView.frame = self.view.bounds;
    dialogView.model = model;
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:model.smallLogo]];
    dialogView.photo = self.imageView.image;
    dialogView.delegate = self;
    dialogView.titleText = [[NSMutableAttributedString alloc] initWithString:model.nickname];
    dialogView.messageText = [self exampleAttributeString];
    dialogView.firstBtnText = [@"忍痛割爱" uppercaseString];
    dialogView.dialogBackgroundColor = [UIColor whiteColor];
    dialogView.cornerRadius = 8.0;
    dialogView.backgroundShadowOpacity = 0.2;
    dialogView.hideCloseButton = true;
    dialogView.showSecondBtn = false;
    dialogView.contentViewType = SFContentViewTypeDefault;
    dialogView.firstBtnBackgroundColor = [UIColor colorWithRed:0.230 green:0.777 blue:0.316 alpha:1.000];
    [dialogView createBlurBackgroundWithImage:[self jt_imageWithView:self.view] tintColor:[[UIColor blackColor] colorWithAlphaComponent:0.35] blurRadius:60.0];
    
    [self.view addSubview:dialogView];
}

- (NSMutableAttributedString *)exampleAttributeString {
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"您确定取消关注么"];
    [attributedString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:21.0] range:NSMakeRange(3, 2)];
    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:0.230 green:0.777 blue:0.316 alpha:1.000] range:NSMakeRange(3, 2)];
    return attributedString;
}

#pragma mark - Snapshot
- (UIImage *)jt_imageWithView:(UIView *)view {
    CGFloat scale = [[UIScreen mainScreen] scale];
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, scale);
    [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:true];
    UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

#pragma mark - SFDraggableDialogViewDelegate
- (void)draggableDialogView:(SFDraggableDialogView *)dialogView didPressFirstButton:(UIButton *)firstButton {
    LTDataBase *data = [LTDataBase shareLTDataBaseTool];
    [data delegateDataWithModel_id:self.anchorModel.uid type:3];
    [self getData];
    [dialogView dismissWithDrop:YES];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.dataArray.count == 0) {
        self.backImageView.hidden = NO;
    }
    else
    {
        self.backImageView.hidden = YES;
    }
    return self.dataArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LTAnchorCollectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"anchor"];
    cell.model = [self.dataArray objectAtIndex:indexPath.row];
    cell.aBlock = ^(LTAnchorModel *model)
    {
        [self showViewWith:model];
    };
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    YWAnchorIntroduceViewController *anchor = [[YWAnchorIntroduceViewController alloc] init];
    anchor.uid = [[self.dataArray objectAtIndex:indexPath.row] uid];
    anchor.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:anchor animated:YES];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150 * FITHEIGHT;
}


- (void)getData
{
    LTDataBase *data = [LTDataBase shareLTDataBaseTool];
    self.dataArray = [data selectAllWithtype:3];
    [self.tableView reloadData];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
    [self getData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
